var net = require("net");

var server = net.createServer();
var port = 18036;
server.on('connection', function(sock){
    console.log('client connected, address -  ', sock.remoteAddress, ' port - ', sock.remotePort);
    sock.setEncoding('utf8');
    sock.on('data', function(data){
        console.log(data);
        //lBuf.write(data);
        /*var buf = new Buffer(8);
         buf.write(data);
         console.log(buf.toString());
         var num = new Number(buf.toString());
         console.log(num);*/
        console.log('got data from client - ', data);

        sock.write(data);
    });

    sock.on('init',function(data){
        logger.info("接入一个socket客户端");
        logger.info(data);
        client._Id = data.containerId;
        sock.count = 0;

        var command = "uname -a";
        sock.emit('exec', {command:command, cwd:"/app"});
    });

    sock.on('end', function(){
        console.log('client disconnected');
    });

    sock.on('error', function(err){
        console.log('socket error - ', err);
        sock.end();
        sock.destroy();
    });
});

server.maxConnections = 10;
server.listen(port, function(){
    console.log('echo server bound at port - ' + port);
});

server.on('error',function(err){
    console.log("出错了");
    console.log(err);
});